package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.BaseRespVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.cskaoyan.bean.admin.system.AddStorageVo;
import com.cskaoyan.bean.admin.system.StorageBo;
import com.cskaoyan.bean.admin.system.UpdateStorageVo;
import com.cskaoyan.service.admin.StorageService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Classname StorageController
 * @Description
 * @Date 2021/8/12 19:48
 * @Author : xujun
 * @Date 2021/8/12 21:41
 * @Author : songweinan
 */
@RestController
@RequestMapping("admin/storage")
public class StorageController {

    @Autowired
    StorageService storageService;

//    图片上传，徐军版，已过时，=.=#
//    @PostMapping("create")
//    public BaseRespVo storagePic(@RequestParam("file") MultipartFile myFile) throws IOException {
//
//        @Autowired
//        StorageService storageService;
//
//    @PostMapping("create")
//    public BaseRespVo storagePic(@RequestParam("file") MultipartFile myFile) throws IOException {
//
//        String name = myFile.getOriginalFilename();
//        int index = name.indexOf(".");
//        String name1 = name;
//        String substring = name1.substring(index);
//        String uuid = UUID.randomUUID().toString();
//        String key = uuid + substring;
//
//        String type = myFile.getContentType();
//
//        int size = (int) myFile.getSize();
//
//        String resourcePath = System.getProperty("user.dir")+ "\\target\\classes\\static\\";
//        File file = new File(resourcePath + key);
//        myFile.transferTo(file);
//
//        String url = "http://localhost:8083/pic/" + key;
//
//        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//
//        GoodsPicUploadVo vo = new GoodsPicUploadVo();
//        // 不懂这id什么意思，暂时设为null
//        vo.setId(null);
//        vo.setKey(key);
//        vo.setAddTime(format);
//        vo.setUpdateTime(format);
//        vo.setName(name);
//        vo.setType(type);
//        vo.setSize(size);
//        vo.setUrl(url);
//
//        return BaseRespVo.ok(vo);
//    }

    @RequiresPermissions(value = {"*", "admin:storage:list"}, logical = Logical.OR)
    @GetMapping("list")
    public BaseRespVo list(String key, String name, BaseParam param) {


        BaseRespData data = storageService.query(key, name, param);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions(value = {"*", "admin:storage:create"}, logical = Logical.OR)
    @PostMapping("create")
    public BaseRespVo create(@RequestParam("file") MultipartFile uploadFile) {

        AddStorageVo addStorageVo = storageService.create(uploadFile);

        return BaseRespVo.ok(addStorageVo);
    }

    @RequiresPermissions(value = {"*", "admin:storage:delete"}, logical = Logical.OR)
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody StorageBo deleteStorageBo) {

        int deleteCode = storageService.delete(deleteStorageBo);
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = {"*", "admin:storage:update"}, logical = Logical.OR)
    @PostMapping("update")
    public BaseRespVo update(@RequestBody StorageBo updateStorageBo) {

        UpdateStorageVo updateStorageVo = storageService.update(updateStorageBo);
        return BaseRespVo.ok(updateStorageVo);
    }

}
